package com.xdw.ui_demo.recyclerView;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.xdw.ui_demo.R;

import java.util.List;

/**
 * Created by 杨翌东 on 2018/9/20.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<itemViewHolder> {
    private List<Dataitem> dataitemList;//泛型，通过setData向里面传数据
    private OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener{

        void onItemClick(View view,int position);
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){

        this.mOnItemClickListener = mOnItemClickListener;

    }


    public void setData(List<Dataitem> dataitemList){
        this.dataitemList = dataitemList;
        notifyDataSetChanged();              //通知数据有变化，才会传入数据。

    }




    @Override
    public itemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {   //这个函数显示传入列表项，
        //获取LayoutInflater对象，它可以根据布局定义的内容⽣成⼀个View对象
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        //⽣成View对象，
        View itemView = layoutInflater.inflate(R.layout.item_layout, viewGroup, false);
        //实例化⼀个ViewHolder
        itemViewHolder viewHolder = new itemViewHolder(itemView);
        //返回ViewHolder
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(itemViewHolder itemViewHolder, final int i) {  //这个函数显示列表项的内容
        //说明：i是数据项的位置，决定了显示的次序，i==0，就是第⼀条
        //获取第i条数据
        final Dataitem dataItem = dataitemList.get(i);
        //将第i条数据绑定到列表项（View）中。ImageView未做处理，后⾯课程会介绍
        itemViewHolder.textView1.setText(dataItem.getText1());
        itemViewHolder.textView2.setText(dataItem.getText2());

        if (mOnItemClickListener!=null){
            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(),"click:"+i,Toast.LENGTH_SHORT).show();
                    System.out.println("tttaaa:");

                    mOnItemClickListener.onItemClick(view,i);
                }
            });
        }




    }





    @Override
    public int getItemCount() {
        return dataitemList.size();  //j决定看到多少条数据,不能显示返回0.否则不显示数据
    }
}
